{% extends 'xadmin/base.html' %}
{% load staticfiles %}

{% block title %}
    栏目组管理
{% endblock %}

{% block css %}
    <link href="{% static 'xadmin/css/plugins/toastr/toastr.min.css' %}" rel="stylesheet">
{% endblock %}

{% block btn %}
    <button type="button" class="btn btn-xs btn-outline btn-primary" onclick="window.location.href='{% url 'nodegroupadd' %}?siteid={{ siteid }}'">添加栏目组</button>
{% endblock %}

{% block main %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-lg-3 col-lg-offset-9">
                <button type="button" class="btn btn-xs btn-primary" onclick="window.location.href='{% url 'nodegroupadd' %}?siteid={{ siteid }}'"><i class="fa fa-plus"></i>添加栏目组</button>
                <button type="button" class="btn btn-xs btn-danger" onclick="groups_del()"><i class="glyphicon glyphicon-remove"></i>删除</button>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content" style="border-width:0px 0px">
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>
                                            <div class="icheckbox_square-green" style="position: relative;">
                                                <input type="checkbox" class="i-checks checkall" name="input[]" style="position: absolute; opacity: 0;">
                                            </div>
                                        </th>
                                        <th>名称</th>
                                        <th>索引</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for nodegroup in nodegroup_list %}
                                        <tr>
                                            <td>
                                                <div class="icheckbox_square-green" style="position: relative;">
                                                    <input type="checkbox" class="i-checks" value="{{ nodegroup.id }}" name="input[]" style="position: absolute; opacity: 0;">
                                                </div>
                                            </td>
                                            <td>{{ nodegroup.name }}</td>
                                            <td>{{ nodegroup.index_name | default_if_none:'' }}</td>
                                            <td>
                                                <a  class="btn btn-xs btn-outline btn-primary"  href="{% url 'nodegroupedit' nodegroup.id %}?siteid={{ siteid }}">编辑</a>
                                                <a  class="btn btn-xs btn-outline btn-warning" onclick="nodegroup_del({{ nodegroup.id }})">删除</a>
                                            </td>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block script %}
    <script src="{% static 'xadmin/js/plugins/toastr/toastr.min.js' %}"></script>
    <script src="{% static 'xadmin/js/myjs/toastr_customer.js' %}"></script>
    <script>
        $(document).ready(function () {
            $('.i-checks').iCheck({
                checkboxClass: 'icheckbox_square-green dcheckbox',
                radioClass: 'iradio_square-green',
            });
            $('.checkall').on('ifChecked', function(event){
                $('.i-checks:not(:first)').iCheck('check');
            });
            $('.checkall').on('ifUnchecked', function(event){
                $('.i-checks:not(:first)').iCheck('uncheck');
            });
        });
        function nodegroup_del(nodegroupid){
            layer.confirm('确定要删除吗?', function(){
                $.ajax({
                    url: '{% url 'nodegroupdel' %}',
                    type: 'post',
                    dataType: 'json',
                    data: {'nodegroupid': nodegroupid, 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                    success: function(bk){
                        console.log(bk);
                        if(bk.status == '200'){
                            send_success_message(bk.message);
                        }else{
                            toastr.error(bk.message);
                        }
                    }
                })
            })
        }
        //批量删除
        function groups_del(){
            var id_list = [];
            $('.i-checks:not(:first)').each(function(){
                if($(this).get(0).checked){
                    id_list.push($(this).val());
                }
            });
            if(id_list.length > 0){
                layer.confirm('确定要删除吗?', function(){
                    $.ajax({
                        url: '{% url "nodegroupsdel" %}?siteid={{ siteid }}',
                        type: 'post',
                        dataType: 'json',
                        data: {'groupids': id_list.join(','), 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                        success: function(bk){
                            if(bk.status == '200'){
                                send_success_message(bk.msg);
                            }else{
                                toastr.error(bk.msg);
                            }
                        }
                    })
                })

            }else{
                toastr.error('请选择要删除的组');
            }
        }
    </script>

{% endblock %}